package com.example.sharding.common.shardingjdbc.algorithm;

import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.security.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
import java.util.Date;

public class IdPreciseAlgorithm implements PreciseShardingAlgorithm<Date> {
    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<Date> preciseShardingValue) {
        //for (String s : collection) {
        //    System.out.println("表名"+s);
        //}
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
        Date value = preciseShardingValue.getValue();
        String format = sdf.format(value);
        //补充说明：其实tableNames中的值就是配置文件中的actual-data-nodes中配置的所有表，此处返回的是要进行查询的表
        int i = Integer.parseInt(format.substring(format.length() - 2));
        System.out.println("表名后缀i值："+i%2+"user"+i);
        return "user"+i%2;

    }
}
